# coding: utf-8

from urllib.parse import quote_plus
from pymongo import MongoClient


MONGO_HOST = 'dds-uf6291853e669e341.mongodb.rds.aliyuncs.com'
MONGO_PORT = 3717
MONGO_USERNAME = 'root'
MONGO_PASSWORD = 'CfLceH2vCAFc'
MONGO_URI = "mongodb://%s:%s@%s:%s" % (quote_plus(MONGO_USERNAME), quote_plus(MONGO_PASSWORD), MONGO_HOST, MONGO_PORT)


mongo = MongoClient(MONGO_URI)
mongo_database = mongo.foreign
mongo_collection = mongo_database.recommend
print(mongo_collection.insert({
    '_id': 1,
    'keywords': [
        {
            'words': '中国',
            'score': 1.2
        },
        {
            'words': 'china',
            'score': 1.2
        },
        {
            'words': '美国',
            'score': 2.2
        },
        {
            'words': 'US',
            'score': 2.2
        }
    ]
}))

print(mongo_collection)

# user_model = mongo_collection.find_one({'_id': 1})
# if not user_model:
#     mongo_collection.insert({'_id': 1, 'keywords': keywords})


# user_keywords = user_model['keywords']
# print(user_keywords)
#
#
# keywords = [{'words': '中国', 'score': 1.2}, {'words': 'china', 'score': 1.2}, {'words': '美国', 'score': 2.2}, {'words': 'US', 'score': 2.2}]
# current_word_list = [{'words': '中国', 'score': 10.5}]
#
# new_keywords = []
# for words_dict in keywords:
#     words = words_dict['words']
#     score = words_dict['score']
#     for current_words_dict in current_word_list:
#         current_words = current_words_dict['words']
#         current_score = current_words_dict['score']
#         if words == current_words:
#             score += current_score
#         new_keywords.append({'words': words, 'score': score})
#
#
# print(new_keywords)
#
# update_result = mongo_collection.update_one(filter={'_id': 1}, update={'$set': {'keywords': new_keywords}})
# print(update_result.modified_count)
